package cn.coderliu.voteweb.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {

    public static Date getTheDayAfterTomorrow(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_YEAR, 2);
        return cal.getTime();
    }

    public static Date getTheDayAfterNDays(Date date, int n) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_YEAR, n);
        return cal.getTime();
    }

    public static Date getNextDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_YEAR, 1);
        return cal.getTime();
    }

    public static String formatToMonthStr(Date d) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
        return df.format(d);
    }

    public static Date parseMonthStr(String y_m) throws ParseException {


        if (y_m.indexOf("-") > -1) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
            return df.parse(y_m);
        } else if (y_m.indexOf("/") > -1) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy/MM");
            return df.parse(y_m);
        } else {
            SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
            return df.parse(y_m);
        }

    }

    public static Date parseTimeStr(String h_m) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");
        return df.parse(h_m);
    }

    public static Date parseTimeStr_hms(String h_m) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        return df.parse(h_m);
    }

    public static Date parseDateStr(String y_m_d) throws ParseException {
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            return df.parse(y_m_d);
        } catch (Exception e) {
        }

        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
            return df.parse(y_m_d);
        } catch (Exception e) {
        }

        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
            return df.parse(y_m_d);
        } catch (Exception e) {
        }
        return null;
    }

    public static Date parseDateTimeStr(String str) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.parse(str);
    }

    public static String formatToDateStr(Date date) throws ParseException {
        if (date == null) {
            return "";
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.format(date);
    }

    public static String formatToYYMM(Date date) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
        return df.format(date);
    }

    public static String formatToDateTimeStr(Date date) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(date);
    }

    public static String formatToTimeStr(Date date) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        return df.format(date);
    }

    public static Date getMonthBegin(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1, 0, 0, 0);
        return cal.getTime();
    }

    public static Date getMonthBegin(String year, String month) {
        Calendar cal = Calendar.getInstance();
        // cal.setTime(new Date());
        cal.set(Integer.parseInt(year), Integer.parseInt(month) - 1, 1, 0, 0, 0);
        return cal.getTime();
    }

    public static Date getMonthEnd(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, 1, 0, 0, 0);
        cal.set(Calendar.SECOND, cal.get(Calendar.SECOND) - 1);

        return cal.getTime();
    }

    public static Date getWeekBegin(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        Date mm = nDaysAgo(cal.get(Calendar.DAY_OF_WEEK) - 2, date);
        return getDayBegin(mm);
    }

    public static Date getWeekEnd(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        Date mm = nDaysAfter(cal.get(8 - Calendar.DAY_OF_WEEK), date);
        return getDayEnd(mm);

    }

    public static Date nDaysAfter(int n, Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + n);
        return cal.getTime();
    }

    public static Date nMinutesAfter(int n, Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE) + n);
        return cal.getTime();
    }

    public static Date nSecondsAfter(int n, Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.SECOND, cal.get(Calendar.SECOND) + n);
        return cal.getTime();
    }

    public static Date getDayBegin(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        try {
            return dateFormat.parse(dateFormat.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date getDayEnd(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
            cal.get(Calendar.DAY_OF_MONTH) + 1, 0, 0, 0);
        cal.set(Calendar.SECOND, cal.get(Calendar.SECOND) - 1);

        return cal.getTime();
    }

    public static Date nMonthsAgo(Integer n, Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - n);
        return cal.getTime();
    }

    public static Date nDaysAgo(Integer n, Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - n);
        return cal.getTime();
    }

    public static int getDayOfWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_WEEK);
    }

    public static int getDayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_MONTH);
    }

    public static Date getNowTime() {
        DateFormat df = new SimpleDateFormat("HH:mm");

        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(df.parse(df.format(new Date())));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return cal.getTime();
    }

    public static int getYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.YEAR);
    }

    public static int getMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.MONTH) + 1;
    }

    /**
     * 时间格式字符串转换成Date类型
     *
     * @param date
     * @param format 如：yyyy-MM-dd HH:mm:ss
     * @return
     * @author: NQY
     * @Createtime: Sep 14, 2012
     */
    public static Date dateStrConvertDate(String date, String format) {
        try {
            SimpleDateFormat simpledateformat = new SimpleDateFormat(format);
            Date newdate = simpledateformat.parse(date);
            return newdate;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 时间格式得到年月日字符串 yyyyMMddHHmmss
     *
     * @param date
     * @return
     */
    public static String getDateStr(Date date) {
        try {
            SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMddHHmmss");
            return simpledateformat.format(date);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 根据给出的年份，获得年份至今年的倒序list
     * @param startyear
     * @return
     */
    public static List<Integer> getyearlist(Integer startyear){
        List<Integer> yearlist = new ArrayList<Integer>();
        Calendar c= Calendar.getInstance();
        for(int i = c.get(Calendar.YEAR); i>=startyear; i--){
            yearlist.add(i);
        }
        return yearlist;
    }


    public static List<Integer> getLatelyYears(Date date, int count) {
        List<Integer> years = new ArrayList<Integer>();
        Integer year = getYear(date);
        for (int i = 0; i < count; i++) {
            years.add(year - i);
        }
        return years;
    }

    public static List<String> getMonths(){
        List<String> months = new ArrayList<String>();
        for(int i=1;i<=12;i++){
            months.add(i<10?"0"+i:i+"");
        }
        return months;
    }
    /**
     * 将Date类型转换成字符串类型
     *
     * @param  date
     * @param  format 如：yyyy-MM-dd HH:mm:ss
     * @return
     * @author: NQY
     * @Createtime: Sep 14, 2012
     */
    public static String dateConvertStr(Date date, String format) {
        return (date == null) ? null : new SimpleDateFormat(format)
                .format(date);
    }
    public static void main(String[] args) {


    }
}
